import { pythonGenerator } from 'blockly/python';
import * as python from 'blockly/python';

pythonGenerator.forBlock['set_device_prop'] = function (block, generator) {
    var value_device = generator.valueToCode(block, 'device', python.Order.ATOMIC);
    var value_sid = generator.valueToCode(block, 'sid', python.Order.ATOMIC);
    var value_pid = generator.valueToCode(block, 'pid', python.Order.ATOMIC);
    var value_value = generator.valueToCode(block, 'value', python.Order.ATOMIC);
    var code = value_device + '.set_device_prop(' + value_sid + ', ' + value_pid + ', ' + value_value + ')\n';
    return code;
};

pythonGenerator.forBlock['find_device'] = function (block, generator) {
    var value_device_name = generator.valueToCode(block, 'device_name', python.Order.ATOMIC);
    var code = 'mi.find_device(' + value_device_name + ')';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['use_device'] = function (block, generator) {
    var value_device_id = generator.valueToCode(block, 'device_id', python.Order.ATOMIC);
    var code = 'mi.use_device(' + value_device_id + ')';
    return [code, python.Order.ATOMIC];
};


pythonGenerator.forBlock['sleep'] = function (block, generator) {
    var value_second = generator.valueToCode(block, 'second', python.Order.ATOMIC);
    var code = 'time.sleep(' + value_second + ')\n';
    return code;
};

pythonGenerator.forBlock['do_action'] = function (block, generator) {
    var value_device = generator.valueToCode(block, 'device', python.Order.ATOMIC);
    var value_sid = generator.valueToCode(block, 'sid', python.Order.ATOMIC);
    var value_aid = generator.valueToCode(block, 'aid', python.Order.ATOMIC);
    var value_value = generator.valueToCode(block, 'value', python.Order.ATOMIC);
    if (value_value)
        var code = value_device + '.do_action(' + value_sid + ', ' + value_aid + ', ' + value_value + ')\n';
    else
        var code = value_device + '.do_action(' + value_sid + ', ' + value_aid + ')\n';
    return code;
};

pythonGenerator.forBlock['get_device_prop'] = function (block, generator) {
    var value_device = generator.valueToCode(block, 'device', python.Order.ATOMIC);
    var value_sid = generator.valueToCode(block, 'sid', python.Order.ATOMIC);
    var value_pid = generator.valueToCode(block, 'pid', python.Order.ATOMIC);
    var code = value_device + '.get_device_prop(' + value_sid + ', ' + value_pid + ')\n';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['create_datetime'] = function (block, generator) {
    var code = 'datetime.datetime.now()\n';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_year'] = function (block, generator) {
    var value_datetime = generator.valueToCode(block, 'datetime', python.Order.ATOMIC);
    var code = value_datetime.replaceAll("\n", "") + '.year';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_month'] = function (block, generator) {
    var value_datetime = generator.valueToCode(block, 'datetime', python.Order.ATOMIC);
    var code = value_datetime.replaceAll("\n", "") + '.month';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_day'] = function (block, generator) {
    var value_datetime = generator.valueToCode(block, 'datetime', python.Order.ATOMIC);
    var code = value_datetime.replaceAll("\n", "") + '.day';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_hour'] = function (block, generator) {
    var value_datetime = generator.valueToCode(block, 'datetime', python.Order.ATOMIC);
    var code = value_datetime.replaceAll("\n", "") + '.hour';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_minute'] = function (block, generator) {
    var value_datetime = generator.valueToCode(block, 'datetime', python.Order.ATOMIC);
    var code = value_datetime.replaceAll("\n", "") + '.minute';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_second'] = function (block, generator) {
    var value_datetime = generator.valueToCode(block, 'datetime', python.Order.ATOMIC);
    var code = value_datetime.replaceAll("\n", "") + '.second';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_weekDayStr'] = function (block, generator) {
    var code = '["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"][datetime.date.today().weekday()]\n';
    return [code, python.Order.ATOMIC];
};

pythonGenerator.forBlock['get_weekDay'] = function (block, generator) {
    var code = 'datetime.date.today().weekday()\n';
    return [code, python.Order.ATOMIC];
};